//
//  LFTMCUpdateTeamNameViewController.m
//  lofter-mvpcourse-ios
//
//  Created by Thomas Quan on 2017/5/3.
//  Copyright © 2017年 undefined-zola. All rights reserved.
//

#import "LFTMCUpdateTeamNameViewController.h"
#import "Masonry.h"
#import "UIView+Toast.h"
#import "LFTMCTitleMaker.h"
#import "NIMGlobalMacro.h"
#import "UIView+NIM.h"

#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@interface LFTMCUpdateTeamNameViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *doneBtn;

@property (nonatomic, assign) BOOL isBounce;

@end

@implementation LFTMCUpdateTeamNameViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.isBounce = NO;
    //监听键盘弹出事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //监听键盘隐藏事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    //获取键盘信息
    NSDictionary *keyBoardInfo = [notification userInfo];
    
    //获取动画时间
    CGFloat duration = [[keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    if (self.textField.isFirstResponder && !self.isBounce) {
        [UIView animateWithDuration:duration animations:^{
            CGRect frame = self.textField.frame;
            frame.origin.y = self.textField.frame.origin.y - 100;
            self.textField.frame = frame;
            
            frame = self.lineView.frame;
            frame.origin.y = self.lineView.frame.origin.y - 100;
            self.lineView.frame = frame;
            
            frame = self.cancelBtn.frame;
            frame.origin.y = self.cancelBtn.frame.origin.y - 100;
            self.cancelBtn.frame = frame;
            
            frame = self.doneBtn.frame;
            frame.origin.y = self.doneBtn.frame.origin.y - 100;
            self.doneBtn.frame = frame;
            
            [self.view layoutIfNeeded];
        } completion:nil];
        self.isBounce = YES;
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    //获取键盘信息
    NSDictionary *keyBoardInfo = [notification userInfo];
    
    //获取动画时间
    CGFloat duration = [[keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    //获取键盘的frame信息
    [UIView animateWithDuration:duration animations:^{
        CGRect frame = self.textField.frame;
        frame.origin.y = self.view.nim_centerY - 30 - frame.size.height/2;
        self.textField.frame = frame;
        
        frame = self.lineView.frame;
        frame.origin.y = self.textField.nim_bottom;
        self.lineView.frame = frame;
        
        frame = self.cancelBtn.frame;
        frame.origin.y = self.lineView.nim_bottom + 30;
        self.cancelBtn.frame = frame;
        
        frame = self.doneBtn.frame;
        frame.origin.y = self.lineView.nim_bottom + 30;
        self.doneBtn.frame = frame;
        
        [self.view layoutIfNeeded];
    } completion:nil];
    self.isBounce = NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textField endEditing:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"修改群名称";
    self.textField = [[UITextField alloc] init];
    self.textField.delegate = self;
    self.textField.text = self.team.teamName;
    self.textField.textAlignment = NSTextAlignmentLeft;
    self.textField.textColor = [UIColor blackColor];
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.textField.adjustsFontSizeToFitWidth = YES;
    self.textField.returnKeyType = UIReturnKeyDone;
    
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = NIMKit_UIColorFromRGB(0xEEEEEE);
    
    self.cancelBtn = [[UIButton alloc] init];
    [self.cancelBtn setTitle: @"取消" forState: UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.cancelBtn setTitleColor:NIMKit_UIColorFromRGB(0xCCCCCC) forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(cancelOpration) forControlEvents:UIControlEventTouchUpInside];
    
    self.doneBtn = [[UIButton alloc] init];
    [self.doneBtn setTitle: @"完成" forState: UIControlStateNormal];
    self.doneBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.doneBtn setTitleColor:NIMKit_UIColorFromRGB(0x97CC00) forState:UIControlStateNormal];
    [self.doneBtn addTarget:self action:@selector(updateTeamName) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.textField];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.cancelBtn];
    [self.view addSubview:self.doneBtn];
    
    [self.view layoutIfNeeded];
}

- (void)close {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupNav {
    UIBarButtonItem *backButtonItem =
    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"LFTMC_Back"]
                                     style:UIBarButtonItemStylePlain
                                    target:self
                                    action:@selector(close)];
    [backButtonItem setTintColor:NIMKit_UIColorFromRGB(0x333333)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    CGFloat width = self.view.frame.size.width;
    
    self.textField.nim_centerY = self.view.nim_centerY - 30;
    self.textField.nim_left = 30;
    self.textField.nim_width = width - 30 * 2;
    self.textField.nim_height = 30;
    
    self.lineView.nim_top = self.textField.nim_bottom;
    self.lineView.nim_height = 1;
    self.lineView.nim_left = 30;
    self.lineView.nim_width = width - 30 * 2;
    
    self.cancelBtn.nim_left = 100;
    self.cancelBtn.nim_height = 35;
    self.cancelBtn.nim_width = 40;
    self.cancelBtn.nim_top = self.lineView.nim_bottom + 30;
    
    self.doneBtn.nim_right = self.view.nim_right - 100;
    self.doneBtn.nim_height = 35;
    self.doneBtn.nim_width = 40;
    self.doneBtn.nim_top = self.lineView.nim_bottom + 30;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.textField resignFirstResponder];
    return YES;
}

- (void)cancelOpration {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)updateTeamName {
    NSString *name = self.textField.text;
    if (name.length) {
    
        [[NIMSDK sharedSDK].teamManager updateTeamName:name teamId:self.team.teamId completion:^(NSError *error) {
            if (!error) {
                self.team.teamName = name;
                [self.view makeToast:@"修改成功" duration:2
                            position:CSToastPositionCenter];
                [self.navigationController popViewControllerAnimated:YES];
                // 通知上一级刷新数据
                [_delegate reloadTeamDate];
                
            }else{
                [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code] duration:2
                            position:CSToastPositionCenter];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
